package www.dashu.com.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import www.dashu.com.api.CommonResult;
import www.dashu.com.domain.OmsOrder;
import www.dashu.com.dto.OmsOrderQuery;
import www.dashu.com.service.OmsOrderService;
import www.dashu.com.service.RedisService;

import java.util.Date;

@Controller
@Api(tags = "OmsOrderController", description = "订单表")
@RequestMapping("/order")
public class OmsOrderController
{

    @Autowired
    OmsOrderService orderService;
    @Autowired
    RedisService redisService;

    @ApiOperation(value = "订单表列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Page<OmsOrder>> list(OmsOrderQuery query) {

        try {
            Page<OmsOrder> pageData = orderService.findAll(query);
            return CommonResult.success(pageData);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @ApiOperation(value = "新增订单表")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody OmsOrder order) {
        order.setId(orderService.createId(order));

        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());

        orderService.save(order);
        return CommonResult.success();
    }

    @ApiOperation(value = "修改订单表")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody OmsOrder order) {

        order.setUpdateTime(new Date());

        orderService.save(order);
        return CommonResult.success();
    }

    @ApiOperation(value = "删除订单表")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public CommonResult delete(@RequestBody OmsOrder order) {
        orderService.deleteById(order.getId());
        return CommonResult.success();
    }

}
